Reduce Function

The reduce() method is a higher-order function that takes all the elements in a collection (Array, List, etc) and combines them using a binary operation to produce a single value. It is necessary to make sure that operations are commutative and associative. Anonymous functions are passed as parameter to the reduce function. The reduce function works on collection data structures in Scala. The reduce function is applicable to both Scala's Mutable and Immutable collection data structures.

The reduce method takes an associative binary operator function as parameter and will use it to collapse elements from the collection. Unlike the fold method, reduce does not allow you to also specify an initial value. As per the Scala documentation, the definition of the reduce method is as follows:

def reduce[A1 >: A](op: (A1, A1) ⇒ A1): A1

How does reduce Function work in Scala?

// Scala program sum of elements 
// using reduce function 444441

// Creating object
object demo
{
// Main method
def main(arg:Array[String])
{
// initialize a sequence of elements
val seq_elements: Seq[Double] = Seq(3.5, 5.0, 1.5)
//val seq_elements =List(1,2,3,4,5,6,7,8,9,10)
println(s"Elements = $seq_elements")

// find the sum of the elements
// using reduce function
val sum: Double = seq_elements.reduce((a, b) => a + b)
println(s"Sum of elements = $sum")
}
}


object Demo
{
def main(args:Array[String])
{
val collection = List(200 , 400, 100, 30, 1000, 500)
val result = collection.reduce((x1, y1) => x1 + y1)
println(result)
}
}

object Demo
{
def main(args:Array[String])
{
val collection = List(200 , 400, 100, 30, 1000, 500)
val result = collection.reduce((x1, y1) => x1 max y1)
println(result)
}
}
// Scala program to find maximum and minimum
// using reduce function
// Creating object
object demo
{
// Main method
def main(arg:Array[String])
{
// initialize a sequence of elements
val seq_elements : Seq[Double] = Seq(3.5, 5.0, 1.5)
println(s"Elements = $seq_elements")
// find the maximum element using reduce function
val maximum : Double = seq_elements.reduce(_ max _)
println(s"Maximum element = $maximum")
// find the minimum element using reduce function
val minimum : Double = seq_elements.reduce(_ min _)
println(s"Minimum element = $minimum")
}
}



object Demo
{
def main(args:Array[String])
{
val collection = List(200 , 400, 100, 30, 1000, 500)
val result = collection.reduce((x1, y1) => x1 - y1)
println(result)
}
}


object Demo
{
def main(args:Array[String])
{
val collection = List(200 , 400, 100, 30, 1000, 500)
val result = collection.reduce((x1, y1) => x1 * y1)
println(result)
}
}


object Demo
{
def main(args:Array[String])
{
val collection = List(200 , 400, 100, 30, 1000, 500)
val result = collection.reduce((x1, y1) => x1 min y1)
println(result)
}
}



// Scala program sum of elements
// using reduce function

// Creating object
object demo
{
// Main method
def main(arg:Array[String])
{
// initialize a sequence of elements
// val seq_elements: Seq[Double] = Seq(3.5, 5.0, 1.5)
val seq_elements =List(1,2,3,4,5,6,7,8,9,10)
println(s"Elements = $seq_elements")

// find the sum of the elements
// using reduce function
val sum: Double = seq_elements.reduce((a, b) => a + b)
println(s"Sum of elements = $sum")
val iterations: Seq[Int] = seq_elements.scan(0)(_ + _)
println("Running total of all elements" +
s"in the collection = $iterations")
}
}



No comments:

Post a Comment